<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>系统管理--字典管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" href="css/chenwenjing/chenwj.css" type="text/css"></link>
	 <link rel="stylesheet" href="easyui/themes/icon.css" type="text/css"></link>
	 <link rel="stylesheet" href="easyui/themes/default/easyui.css" type="text/css"></link>
	 <script type="text/javascript" src="easyui/jquery-1.8.3.js"></script>
	 <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
     <script type="text/javascript">
     var k = 0;/* 修改回填时候查询出来的加的变量*/
     	 $(function(){ 
     		var i = 0;/* 新增时添加子节点的变量 */
     		var j = 0;/* 修改时添加子节点的变量 */	
     		
     		/* 查询字典表所有记录	*/ 
	  		$("#dg").datagrid({	  		
	  			url:"word/findAll.do",
	  			title:"字典列表",
	  			singleSelect:true,
	  			striped:true,	  			
	  			columns:[[{
	  				title:"ID",
	  				field:"id",
	  				width:100,
	  				align:"center",
	  			},{
	  				title:"名称",
	  				field:"name",
	  				width:395,
	  				align:"center"
	  			},{
	  				title:"操作",
	  				field:"emps",
	  				width:300,
	  				align:"center",
	  				formatter:function (value,row,index){
						return "<a onclick='update("+row.id+")' href='javascript:void(0)'>修改</a>&nbsp;&nbsp;<a onclick='del("+row.id+")' href='javascript:void(0)'>删除</a>";
					}	  				
	  			}]],
	  			toolbar:[{text:"新增",iconCls:"icon-add",handler:function(){
	  				add();
	  			}}]	
	  		});	
	  		/* 新增字典记录 */
	  		$("#dlg").dialog({
		    title: "添加字典",
		    width: 400,
		    height: 400,
		    closed: true,
		    buttons:[{
				text:"添加子节点",
				handler:function(){
	  			i++;
	  			$("#addzi").append("<div id=div"+i+"><span>名称</span><input type='text' id='add"+i+"'></div>");	  			
				}
			},{
				text:"删除子节点",
				handler:function(){
	  			$("#div"+i).remove();  
		  		i--;
				}
			},{
				text:"保存",
				handler:function(){				
					var names = new Array();
					for(var a=1;a<=i;a++){
						names.push($("#add"+a).val());
					}
					var data ="name="+$("#name").val()+"&names="+names;
					$.get("word/add.do",data,function(data){
						alert(data);
						$("#dlg").dialog("close");
						$("#dg").datagrid("reload");
					},"text");  	
				}
			},{
				text:"取消",
				handler:function(){
					$("#dlg").dialog("close");
				}
			}]
		});	
	  	/* 修改字典 */
		$("#dlg1").dialog({
		    title: "修改字典",
		    cache:false, 
		    width: 400,
		    height: 400,
		    closed: true,
		    buttons:[{
				text:"添加子节点",
				handler:function(){
	  			j++;
	  			$("#eee").append("<div id=div"+j+"><span>名称</span><input type='text' id='update"+j+"'></div>");	  			
				}
			},{
				text:"删除子节点",
				handler:function(){
	  			$("#div"+j).remove();  
		  		j--;
				}
			},{
				text:"修改",
				handler:function(){
					var names1 = new Array();
					var namesup = new Array();
					var namesupid = new Array();
					for(var b=1;b<=j;b++){
						names1.push($("#update"+b).val());/*数据库没有的 要更新 */
					}
					for(var d=1;d<=k;d++){
						namesup.push($("#up"+d).val());/*数据库原有的 要更新 */
						namesupid.push($("#upid"+d).val());
					}
					
					var data ="id="+$("#dddid").val()+"&name="+$("#dddname").val()+"&namesupid="+namesupid+"&namesup="+namesup+"&names1="+names1;
		
					$.get("word/update.do",data,function(data){
						alert(data);
						$("#dlg1").dialog("close");
						$("#dg").datagrid("reload");
					},"text");  	
				}
			},{
				text:"取消",
				handler:function(){
					$("#dlg1").dialog("close");
				}
			}]
		});   
		$(".btn").click(function(){
		alert("111");
				$(this).parent().remove(); 		
		});  			
	});
	function add(){
	  	$("#dlg").dialog("open");	  	
	}; 
	/* 修改方法 */
	function update(value){
		$("#dlg1").dialog("open");	
		$.post("word/findById.do","id="+value,function(data){
			$("#dddname").val(data.name);
			$("#dddid").val(data.id);
			$.post("wordchild/findByWordid.do","id="+value,function(data){		
				$("#ddd").empty();
				$("#eee").empty();
				for(var c = 0;c<data.length;c++){	
					var name = data[c].name;
					var id = data[c].id;
					var indexs = data[c].indexs;
					k++;
					$("#ddd").append("<div id='div"+id+"'><span>名称</span><input id='upid"+k+"' type='hidden' value="+id+"><input id='up"+k+"' type='text' value="+name+"><a onclick='delzi("+id+")' href='javascript:void(0)'>删除</a></div>");		
					}  		
		  		},"json");
		},"json");
	};
	function delzi(value){
		$.messager.confirm("提示框", "你确定要删除吗?",function(r){
			if(r){
				var data = "id="+value;
				$.post(
	  				"word/deletechild.do",
	  				data,
	  				function(data){
	  				alert(data);
	  				$("#div"+value).remove();
	  				},
	  				"text"
	  			);
			}
		});
	};
	function del(value){
		$.messager.confirm("提示框", "你确定要删除吗?",function(r){
			if(r){
				var data = "id="+value;
				$.post(
	  				"word/delete.do",
	  				data,
	  				function(data){
	  				alert(data);
	  				$("#dg").datagrid("reload");
	  				},
	  				"text"
	  			);
			}
		});
	};
     </script>
	
  </head>
  
  <body style="height:600px">
	<div id="mainbody">
        <p><img src="css/chenwenjing/img/u89.png" id="img"/>&nbsp;&nbsp;<span>系统设置</span></p>
	    <div class="title">
	        字典管理
	    </div>
	    <table id="dg" ></table> 
	    <div class="content">      
	    <div id="dlg">      		
      			<div id="addzi"><span>名称</span>      			
      			<input type="text" id="name">
      			<hr>
      			</div>
      	</div>
      	<div id="dlg1">    		
      			<div ><span>名称</span>    
      			<input type="hidden" id="dddid">  			
      			<input type="text"  id="dddname">
      			</div>	
      			<div id="ddd"></div>
      			<div id="eee"></div>
      			
      	</div>            
        </div>
        
		
</div>
 </body>
</html>
